package com.daka.controller;

import com.daka.annotation.Login;
import com.daka.entity.Topic;
import com.daka.entity.TopicComment;
import com.daka.enumerate.StatusCode;
import com.daka.enumerate.TopicEnum;
import com.daka.service.TopicService;
import com.daka.vo.response.DataResponseVo;
import com.daka.vo.response.ListDataResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/**
 * @author beliveli on 2016/3/31 0031.
 */
@Controller
@RequestMapping("/topic")
@Slf4j
public class TopicController {

    @Resource(name = "topicService")
    private TopicService topicService;

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/list", consumes = "application/json")
    @Login
    public ListDataResponseVo<Topic> list() {
        List<Topic> topicList = topicService.selectAll();
        return new ListDataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), topicList);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/save", consumes = "application/json")
    @Login
    public DataResponseVo<Topic> save(@Valid @RequestBody Topic topic) {

        Topic result = topicService.save(topic);

        if (result == null) {
            return new DataResponseVo<>(StatusCode.JAVA_THROW_EXCEPTION.getStatusCode(), StatusCode.JAVA_THROW_EXCEPTION.getStatusMessage());
        }

        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), result);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/info", consumes = "application/json")
    @Login
    public DataResponseVo<Topic> info(@Valid @RequestBody Topic topic) {

        Topic result = topicService.selectById(topic.getId());

        if (result == null) {
            return new DataResponseVo<>(StatusCode.JAVA_THROW_EXCEPTION.getStatusCode(), StatusCode.JAVA_THROW_EXCEPTION.getStatusMessage());
        }

        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), result);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/effective", consumes = "application/json")
    @Login
    public DataResponseVo<Topic> effective(@Valid @RequestBody Topic topic) {

        topic.setDisplay(TopicEnum.DISTORY_YES.getStatusCode());
        Topic result = topicService.updateDisplay(topic);
        if (result == null) {
            return new DataResponseVo<>(StatusCode.JAVA_THROW_EXCEPTION.getStatusCode(), StatusCode.JAVA_THROW_EXCEPTION.getStatusMessage());
        }

        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), result);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/invalid", consumes = "application/json")
    @Login
    public DataResponseVo<Topic> invalid(@Valid @RequestBody Topic topic) {

        topic.setDisplay(TopicEnum.DISTORY_NO.getStatusCode());
        Topic result = topicService.updateDisplay(topic);
        if (result == null) {
            return new DataResponseVo<>(StatusCode.JAVA_THROW_EXCEPTION.getStatusCode(), StatusCode.JAVA_THROW_EXCEPTION.getStatusMessage());
        }

        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), result);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/updateTop", consumes = "application/json")
    @Login
    public DataResponseVo<Topic> updateTop(@Valid @RequestBody Topic topic) {

        topic.setDisplay(TopicEnum.DISTORY_NO.getStatusCode());
        Topic result = topicService.updateTop(topic);
        if (result == null) {
            return new DataResponseVo<>(StatusCode.JAVA_THROW_EXCEPTION.getStatusCode(), StatusCode.JAVA_THROW_EXCEPTION.getStatusMessage());
        }

        return new DataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), result);
    }

    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/comment", consumes = "application/json")
    @Login
    public ListDataResponseVo<TopicComment> comment(@Valid @RequestBody Topic topic) {

        List<TopicComment> result = topicService.selectCommentById(topic.getId());
        if (result == null) {
            return new ListDataResponseVo<>(StatusCode.JAVA_THROW_EXCEPTION.getStatusCode(), StatusCode.JAVA_THROW_EXCEPTION.getStatusMessage());
        }

        return new ListDataResponseVo<>(StatusCode.SUCCESS.getStatusCode(), StatusCode.SUCCESS.getStatusMessage(), result);
    }
}
